import QtQuick
import QtQuick.Window

Item {
    property int enableSize: 4
    property bool isPressed: false
    property point customPoint

    //左上角
    Item {
        id: leftTop
        width: enableSize
        height: enableSize
        x: 0
        y: 0
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeFDiagCursor:Qt.ArrowCursor;
            onReleased: release()
            onPositionChanged: function(mouse) { positionChange(mouse, -1, -1) }
        }
    }

    //Top
    Item {
        id: top
        width: parent.width - enableSize * 2
        height: enableSize
        x: enableSize
        y: 0
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeVerCursor:Qt.ArrowCursor;
            onReleased: release()

            onMouseYChanged: positionChange(Qt.point(customPoint.x, mouseY), 1, -1)
        }
    }

    //右上角
    Item {
        id: rightTop
        width: enableSize
        height: enableSize
        x: parent.width - enableSize
        y: 0
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) {
                press(mouse);
            }
            cursorShape: containsMouse?Qt.SizeBDiagCursor:Qt.ArrowCursor;
            onReleased: release()
            onPositionChanged: function(mouse) { positionChange(mouse, 1, -1) }
        }
    }

    //Left
    Item {
        id: left
        width: enableSize
        height: parent.height - enableSize * 2
        x: 0
        y: enableSize
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeHorCursor:Qt.ArrowCursor;
            onReleased: release()

            onMouseXChanged: positionChange(Qt.point(mouseX, customPoint.y), -1, 1)
        }
    }

//    //Center - 5
//    Item {
//        id: center
//        anchors.left: left.right
//        anchors.right: right.left
//        anchors.top: top.bottom
//        anchors.bottom: bottom.top
//        MouseArea {
//            anchors.fill: parent

//            property point clickPos
//            onPressed: clickPos = Qt.point(mouse.x,mouse.y)
//            onPositionChanged: {
//                if(pressed && root.visibility !== Window.Maximized && root.visibility !== Window.FullScreen) {
//                    var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
//                    root.x += delta.x
//                    root.y += delta.y
//                }
//            }
//        }
//    }

    //Right
    Item {
        id: right
        width: enableSize
        height: parent.height - enableSize * 2
        x: parent.width - enableSize
        y: enableSize
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeHorCursor:Qt.ArrowCursor;
            onReleased: release()

            onMouseXChanged: positionChange(Qt.point(mouseX, customPoint.y), 1, 1)
        }
    }

    //左下角
    Item {
        id: leftBottom
        width: enableSize
        height: enableSize
        x: 0
        y: parent.height - enableSize
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeBDiagCursor:Qt.ArrowCursor;
            onReleased: release()

            onPositionChanged: function(mouse) { positionChange(mouse, -1, 1) }
        }
    }

    //bottom
    Item {
        id: bottom
        width: parent.width - enableSize * 2
        height: enableSize
        x: enableSize
        y: parent.height - enableSize
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true

            onPressed: function(mouse) { press(mouse) }
            cursorShape: containsMouse?Qt.SizeVerCursor:Qt.ArrowCursor;
            onReleased: release()

            onMouseYChanged: positionChange(Qt.point(customPoint.x, mouseY), 1, 1)
        }
    }

    //右下角
    Item {
        id:rightBottom
        width: enableSize
        height: enableSize
        x: parent.width - enableSize
        y: parent.height - enableSize
        z: 1000
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            cursorShape: containsMouse?Qt.SizeFDiagCursor:Qt.ArrowCursor;
            onPressed: function(mouse) { press(mouse) }
            onReleased: release()

            onPositionChanged: function(mouse) { positionChange(mouse,1,1) }
        }
    }

    function press(mouse) {
        isPressed = true
        customPoint = Qt.point(mouse.x, mouse.y)
    }

    function release() {
        isPressed = false
        //customPoint = undefined
    }

    function positionChange(newPosition, directX/*x轴方向*/, directY/*y轴方向*/) {
        if(!isPressed) return

        var delta = Qt.point(newPosition.x-customPoint.x, newPosition.y-customPoint.y)
        var tmpW,tmpH

        if(directX >= 0)
            tmpW = root.width + delta.x
        else
            tmpW = root.width - delta.x

        if(directY >= 0)
            tmpH = root.height + delta.y
        else
            tmpH = root.height - delta.y

        if(tmpW < root.minimumWidth) {
            if(directX < 0)
                root.x += (root.width - root.minimumWidth)
            root.width = root.minimumWidth
        }
        else {
            root.width = tmpW
            if(directX < 0)
                root.x += delta.x
        }

        if(tmpH < root.minimumHeight) {
            if(directY < 0)
                root.y += (root.height - root.minimumHeight)
            root.height = root.minimumHeight
        }
        else {
            root.height = tmpH
            if(directY < 0)
                root.y += delta.y
        }
    }
}
